package com.qdaily.ui.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.qdaily.supportlibrary.request.JsonData;
import com.qdaily.supportlibrary.util.ImageUtil;
import com.qdaily.supportlibrary.util.NetUtil;
import com.qdaily.supportlibrary.util.QDJsonUtil;
import com.qdaily.ui.MainActivity;
import com.qdaily.ui.PhotoChoose.CropOption;
import com.qdaily.ui.PhotoChoose.PhotoReadyHandler;
import com.qdaily.ui.PhotoChoose.SelectPhotoManager;
import com.qdaily.ui.QDBaseActivity;
import com.qdaily.ui.R;
import com.qdaily.ui.data.UserInformation;
import com.qdaily.ui.net.NetConfigs;
import com.qdaily.ui.net.entity.UserCenterEntity;
import com.qdaily.ui.ui.views.QDWebView.QDArticleWebViewFileCache.QDArticleWebViewFileCacheMgr;
import com.qdaily.ui.ui.views.TitleBar;
import com.qdaily.ui.util.BusProvider;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 完善注册
 * Created with IntelliJ IDEA.
 * User: Felix
 * Date: 4/13/15
 * Time: 22:44
 * QQ:74104
 * Email:lovejiuwei@gmail.com
 */
public class RegisterNextStepActivity extends QDBaseActivity implements View.OnClickListener {
    public static final String KEY_REGISTER_EMAIL = "KEY_REGISTER_EMAIL";
    public static final String KEY_REGISTER_PASSWORD = "KEY_REGISTER_PASSWORD";
    private String email = "";
    private String password = "";
    private File imageFile;

    private ImageView cubeImageViewRegisterNextStepPhoto;
    private android.widget.EditText editTextRegisterNextStepNickName;
    private android.widget.EditText editTextRegisterNextStepNickDes;
    private android.widget.TextView textViewRegisterNextStepDone;
    private TitleBar viewTitleHeaderBar;

    //选择图片、拍照回调。
    private final int REQUEST_IMAGE = 0x22;
    private final int REQUEST_CAMERA = 0x33;
    private final int REQUEST_IMAGESHOW = 0x44;
    private final int REQUEST_IMAGECROP = 0x55;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_register_next_step);
        initViewRes();
    }

    private void initViewRes() {
        this.textViewRegisterNextStepDone = (TextView) findViewById(R.id.textViewRegisterNextStepDone);
        this.editTextRegisterNextStepNickDes = (EditText) findViewById(R.id.editTextRegisterNextStepNickDes);
        this.editTextRegisterNextStepNickName = (EditText) findViewById(R.id.editTextRegisterNextStepNickName);
        this.cubeImageViewRegisterNextStepPhoto = (ImageView) findViewById(R.id.cubeImageViewRegisterNextStepPhoto);
        this.textViewRegisterNextStepDone.setOnClickListener(this);
        this.cubeImageViewRegisterNextStepPhoto.setOnClickListener(this);
        this.viewTitleHeaderBar = (TitleBar) findViewById(R.id.viewTitleHeaderBar);
        this.viewTitleHeaderBar.setTitle(R.string.register_next_step);
        this.viewTitleHeaderBar.onBack();

        this.email = getIntent().getStringExtra(KEY_REGISTER_EMAIL);
        this.password = getIntent().getStringExtra(KEY_REGISTER_PASSWORD);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.textViewRegisterNextStepDone:
                iWantRegister();
                break;
            case R.id.cubeImageViewRegisterNextStepPhoto:
                getImage();
                break;
        }
    }

    private void iWantRegister() {
        String nikeName = editTextRegisterNextStepNickName.getText().toString();
        if (TextUtils.isEmpty(nikeName)) {
            displayToast(R.string.register_need_nickname);
            return;
        }
        String description = editTextRegisterNextStepNickDes.getText().toString();

        getRequestManager().requestRegister(email, password, description, nikeName, imageFile);
    }

    SelectPhotoManager selectPhotoManager;
    private void getImage() {
        if (selectPhotoManager == null)
        {
            selectPhotoManager = new SelectPhotoManager();
        }

        CropOption cropOption = new CropOption();
        cropOption.style = CropOption.CropStyle.CropStyleDefault; //圆形
        selectPhotoManager.setCropOption(cropOption);
        selectPhotoManager.start(RegisterNextStepActivity.this, new SelectPhotoManager.SelectClickHandler() {
            @Override
            public void onClick(int action) {
                if (action == SelectPhotoManager.ACTION_TAKE_PHOTO) {
                } else if (action == SelectPhotoManager.ACTION_ALBUM) {
                } else if (action == SelectPhotoManager.ACTION_CANCEL) {
                }
            }
        });

        selectPhotoManager.setPhotoReadyHandler(new PhotoReadyHandler() {
            @Override
            public void onPhotoReady(int from, String imgPath) {
                if (from == PhotoReadyHandler.FROM_CAMERA) {
                } else if (from == PhotoReadyHandler.FROM_ALBUM) {
                } else if (from == PhotoReadyHandler.FROM_CROP) {
                    ImageUtil.getInstance().displayLocalRoundedImage(imgPath, cubeImageViewRegisterNextStepPhoto);
                    imageFile = new File(imgPath);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (selectPhotoManager != null)
            selectPhotoManager.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onRequestCallBack(NetUtil.NetParams params, JsonData response) {
        super.onRequestCallBack(params, response);

        if (params instanceof NetConfigs.RequestRegister) {
            UserCenterEntity a = QDJsonUtil.Json2Object(response.toString(), UserCenterEntity.class);
            if (a.getMeta().getStatus() == NetConfigs.HTTP_RESPONSE_200) {
                //2 save user obj
                UserInformation userInformation = new UserInformation().translation(a);
                String userInformationJson = QDJsonUtil.getJsonString(userInformation);
                boolean isSaveCompleted = getUserInformationManager().updateUserInformation(userInformationJson);
                if (isSaveCompleted) {
                    //更新cookie
                    NetConfigs.getCookie(getUserInformationManager().getUserInformation().getCookie());
                    //update observer
                    BusProvider.getInstance().post(userInformation);
                    //3 show main
                    Intent displayMainIntent = new Intent(RegisterNextStepActivity.this, MainActivity.class);
                    displayMainIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(displayMainIntent);
                }
            } else {
                displayToast(a.getMeta().getMsg());
            }
        }
    }
}
